package com.wwj.study.mybatis.demo;

import com.wwj.study.mybatis.bean.Member;
import com.wwj.study.mybatis.bean.MemberLazy;
import com.wwj.study.mybatis.bean.MemberLog;
import com.wwj.study.mybatis.mapper.MemberLogMapper;
import com.wwj.study.mybatis.mapper.MemberMapper;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;

/**
 * @Author: wuweijun
 * @Date: 2018/12/12 19:04
 */
public class Demo {

    private static SqlSession getSqlSession() {
        SqlSessionFactory factory= getSqlSessionFactory();
        return factory.openSession();
    }

    private static SqlSessionFactory getSqlSessionFactory(){
        InputStream inputStream= null;
        try {
            inputStream = new FileInputStream("F:\\2018\\wwj\\mybatis\\src\\main\\java\\com\\wwj\\study\\mybatis\\demo\\mybatisConfig.xml");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        SqlSessionFactory factory= new SqlSessionFactoryBuilder().build(inputStream);

        return factory;
    }


    public static void main(String[] args) {
//        Long id = testInsert();
//        testQuery(id);

//        testCache2(1L);

//        testCache(1L);

        testLazy(1L);
    }

    private static Member testQuery(Long id){
        SqlSession sqlSession = getSqlSession();
        MemberMapper memberMapper = sqlSession.getMapper(MemberMapper.class);

        Member s= memberMapper.selectByPrimaryKey(id);
        System.out.println(s.toString());

        return s;
    }


    private static Long testInsert() {
        Member member = new Member();
        member.setName("wwj");
        member.setAddr("beijing");
        member.setAge(100);

        SqlSession sqlSession = getSqlSession();
        MemberMapper memberMapper = sqlSession.getMapper(MemberMapper.class);
        Long id = memberMapper.insert(member);
        //需要commit
        sqlSession.commit();
        System.out.println("插入ID:" + id);
        return id;
    }

    //测试一级缓存,一级缓存是SqlSession级别的，在一个会话期间内有效
    private static void testCache(Long id) {

        SqlSession sqlSession = getSqlSession();
        MemberMapper memberMapper = sqlSession.getMapper(MemberMapper.class);

        Long startTime = System.currentTimeMillis();
        Member s1 = memberMapper.selectByPrimaryKey(id);
        System.out.println("耗时：" + (System.currentTimeMillis() - startTime));
        System.out.println(s1);

        s1.setName("update wwj");
        memberMapper.update(s1);

        Long startTime1 = System.currentTimeMillis();
        Member s2 = memberMapper.selectByPrimaryKey(id);
        System.out.println("耗时：" + (System.currentTimeMillis() - startTime1));
        System.out.println(s2);

        sqlSession.commit();
        sqlSession.close();

    }

    //测试二级缓存,二级缓存Mapper级别
    private static void testCache2(Long id) {
        //必须来自是同一个工厂
        SqlSessionFactory factory=getSqlSessionFactory();

        SqlSession sqlSession1 = factory.openSession();
        MemberMapper memberMapper1 = sqlSession1.getMapper(MemberMapper.class);

        Long startTime = System.currentTimeMillis();
        Member s1 = memberMapper1.selectByPrimaryKey(id);
        System.out.println("耗时：" + (System.currentTimeMillis() - startTime));
        System.out.println(s1);


        //整理更新Log数据后，下面的查询还是扫描的历史数据。会产生脏读
        MemberLogMapper memberLogMapper = sqlSession1.getMapper(MemberLogMapper.class);
        MemberLog memberLog=new MemberLog();
        memberLog.setId(1L);
        memberLog.setName("update wwj");
        memberLogMapper.update(memberLog);

        sqlSession1.commit();
        sqlSession1.close();

        SqlSession sqlSession2 = factory.openSession();
        MemberMapper memberMapper2 = sqlSession2.getMapper(MemberMapper.class);
        Long startTime1 = System.currentTimeMillis();
        Member s2 = memberMapper2.selectByPrimaryKey(id);
        System.out.println("耗时：" + (System.currentTimeMillis() - startTime1));
        System.out.println(s2);
        sqlSession2.commit();
        sqlSession2.close();
    }

    //测试懒加载
    private static void testLazy(Long id) {
        SqlSession sqlSession = getSqlSession();
        MemberMapper memberMapper = sqlSession.getMapper(MemberMapper.class);
        MemberLazy member= memberMapper.selectLazy(id);
        System.out.println(member);

    }
}
